import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:smart_shop/constant/constant.dart';
import 'package:smart_shop/page/order/charge.dart';
import 'package:smart_shop/page/order/worker.dart';
import 'package:smart_shop/utils/colorUtils.dart';

///  订单详情
class OrderList extends StatefulWidget {
  final int index;

  const OrderList(this.index, {super.key});

  @override
  State<StatefulWidget> createState() => _OrderListState();
}

class _OrderListState extends State<OrderList> {
  late Map<String, int> map = <String, int>{};

  @override
  void initState() {
    super.initState();
    map['month'] = 0;
    map['price'] = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BrnAppBar(
          leading: GestureDetector(
            child: Icon(Icons.arrow_back, color: ColorUtils.fromHex('#212121')),
            onTap: () {
              Navigator.of(context).pop();
            },
          ),
          automaticallyImplyLeading: false,
          titleAlignment: Alignment.centerLeft,
          title: Container(
              alignment: Alignment.centerLeft,
              child: Text('订单信息',
                  style: TextStyle(
                      fontSize: 18,
                      fontFamily: 'Source Han Sans CN',
                      color: ColorUtils.fromHex('#212121'),
                      fontWeight: FontWeight.w500))),
          primary: true,
          backgroundColor: Colors.white),
      body: ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: Stack(
          alignment: AlignmentDirectional.topStart,
          textDirection: TextDirection.ltr,
          fit: StackFit.expand,
          children: [
            Flex(
              direction: Axis.vertical,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  flex: 1,
                  child: createListList(context),
                ),
                Container(
                  height: 68,
                  padding:
                      const EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                  color: Colors.white,
                  child: Row(
                    textDirection: TextDirection.ltr,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        width: 117,
                        height: 48,
                        decoration: BoxDecoration(
                            color: ColorUtils.fromHex('#FA5151'),
                            borderRadius: BorderRadius.circular(5)),
                        child: const Center(
                          child: Text('拒绝',
                              style: TextStyle(
                                  fontFamily: 'PingFangSC-Regular',
                                  fontWeight: FontWeight.normal,
                                  fontSize: 18,
                                  color: Colors.white)),
                        ),
                      ),
                      Container(
                        width: 117,
                        height: 48,
                        decoration: BoxDecoration(
                            color: ColorUtils.fromHex('#3D7EFE'),
                            borderRadius: BorderRadius.circular(5)),
                        child: const Center(
                          child: Text('接单',
                              style: TextStyle(
                                  fontFamily: 'PingFangSC-Regular',
                                  fontWeight: FontWeight.normal,
                                  fontSize: 18,
                                  color: Colors.white)),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget createListList(BuildContext context) {
    return ListView(
      scrollDirection: Axis.vertical,
      padding: const EdgeInsets.all(10),
      physics: const BouncingScrollPhysics(),
      children: [
        Container(
          height: 40,
          padding: const EdgeInsets.only(left: 10, right: 10),
          decoration: BoxDecoration(
              border: Border.all(
                  color: const Color.fromRGBO(54, 54, 90, 0.3),
                  width: 1,
                  style: BorderStyle.solid),
              borderRadius: BorderRadius.circular(6),
              color: ColorUtils.fromHex('#F1F2F7')),
          alignment: Alignment.centerLeft,
          child: Text('越亚·天赐良园1-1-3004',
              style: TextStyle(
                  fontFamily: 'PingFangSC-Regular',
                  fontWeight: FontWeight.normal,
                  fontSize: 18,
                  color: ColorUtils.fromHex('#1D1D2E'))),
        ),
        Container(
          height: 44,
          margin: const EdgeInsets.only(top: 10),
          padding: const EdgeInsets.only(left: 10, right: 10),
          decoration: BoxDecoration(
              border: Border.all(
                  color: const Color.fromRGBO(54, 54, 90, 0.3),
                  width: 1,
                  style: BorderStyle.solid),
              borderRadius: BorderRadius.circular(6),
              color: Colors.white),
          child: Row(
            textDirection: TextDirection.ltr,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text('做饭',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C')),
                  overflow: TextOverflow.ellipsis),
              IconButton(
                  onPressed: () {},
                  icon: Icon(
                    Icons.arrow_drop_down,
                    size: 20,
                    color: ColorUtils.fromHex('#B2B2B2'),
                    weight: 500,
                  ))
            ],
          ),
        ),
        Container(
          margin: const EdgeInsets.only(top: 10),
          alignment: AlignmentDirectional.center,
          child: Text('需求描述：',
              style: TextStyle(
                  fontFamily: 'PingFangSC-Regular',
                  fontWeight: FontWeight.normal,
                  fontSize: 18,
                  color: ColorUtils.fromHex('#24242C'))),
        ),
        Container(
            padding: const EdgeInsets.all(10),
            alignment: AlignmentDirectional.topStart,
            decoration: BoxDecoration(
                border: Border.all(
                    color: const Color.fromRGBO(54, 54, 90, 0.3),
                    width: 1,
                    style: BorderStyle.solid),
                borderRadius: BorderRadius.circular(6),
                color: Colors.white),
            child: TextField(
              controller: TextEditingController(text: '每天帮做晚饭即可，一个月'),
              maxLines: 5,
              keyboardType: TextInputType.text,
              decoration: InputDecoration(
                  hintText: '请输入内容',
                  border: InputBorder.none,
                  hintStyle: TextStyle(
                      fontFamily: 'Source Han Sans CN',
                      fontWeight: FontWeight.normal,
                      fontSize: 14,
                      color: ColorUtils.fromHex('#B2B2B2'))),
              style: TextStyle(
                fontFamily: 'Source Han Sans CN',
                fontWeight: FontWeight.normal,
                fontSize: 14,
                color: ColorUtils.fromHex('#1D1D2E'),
              ),
              onChanged: (String value) {},
            )),
        Container(
          margin: const EdgeInsets.only(top: 10, bottom: 10),
          alignment: Alignment.center,
          child: Text('图片/视频：(无)',
              style: TextStyle(
                  fontFamily: 'PingFangSC-Regular',
                  fontWeight: FontWeight.normal,
                  fontSize: 18,
                  color: ColorUtils.fromHex('#002440'))),
        ),
        Align(
          alignment: Alignment.center,
          child: Container(
            width: 200,
            height: 1,
            color: ColorUtils.fromHex('#D8D8D8'),
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          margin: const EdgeInsets.only(top: 10),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('客户',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Text('李海霞',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#1D1D2E')))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('电话',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: '1355809697',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex('#1D1D2E')),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget:
                            Image.asset('${Constant.ICON_URL}mobile.png'),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {}),
                  ))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          margin: const EdgeInsets.only(top: 10),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('服务状态',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Text('待接单',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#B2B2B2')))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('服务师傅',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: '待安排',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex('#B2B2B2')),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget: Icon(Icons.keyboard_arrow_right,
                            color: ColorUtils.fromHex('#959595')),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {
                          Navigator.of(context)
                              .push(MaterialPageRoute(builder: (context) {
                            return const Worker();
                          }));
                        }),
                  ))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('服务开始时间',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: '2022-09-28 09:55',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex('#B2B2B2')),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget: Icon(Icons.keyboard_arrow_right,
                            color: ColorUtils.fromHex('#959595')),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {}),
                  ))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('服务完成时间',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: '2022-09-28 09:55',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex('#B2B2B2')),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget: Icon(Icons.keyboard_arrow_right,
                            color: ColorUtils.fromHex('#959595')),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {}),
                  ))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('服务时长',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: map['month']! > 0 ? '${map['month']}月' : '请选择',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex(getSelectedColor())),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget: Icon(Icons.keyboard_arrow_right,
                            color: ColorUtils.fromHex('#959595')),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      const OrderCharge())).then((data) {
                            setState(() {
                              map['price'] = data['price'];
                              map['month'] = data['month'];
                            });
                          });
                        }),
                  ))
            ],
          ),
        ),
        Container(
          height: 50,
          color: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 10),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            textDirection: TextDirection.ltr,
            children: [
              Text('费用',
                  style: TextStyle(
                      fontFamily: 'PingFangSC-Regular',
                      fontWeight: FontWeight.normal,
                      fontSize: 18,
                      color: ColorUtils.fromHex('#24242C'))),
              Expanded(
                  flex: 1,
                  child: Align(
                    alignment: Alignment.centerRight,
                    child: BrnIconButton(
                        name: map['price']! > 0 ? '${map['price']}元' : '等待计算',
                        widgetHeight: 40,
                        widgetWidth: 200,
                        style: TextStyle(
                            fontFamily: 'PingFangSC-Regular',
                            fontWeight: FontWeight.normal,
                            fontSize: 18,
                            color: ColorUtils.fromHex(getSelectedColor())),
                        direction: Direction.right,
                        mainAxisAlignment: MainAxisAlignment.end,
                        iconWidget: Icon(Icons.keyboard_arrow_right,
                            color: ColorUtils.fromHex('#959595')),
                        iconHeight: 18,
                        iconWidth: 18,
                        onTap: () {
                          if (map['price']! > 0) {
                            openDialog(context);
                          }
                        }),
                  ))
            ],
          ),
        ),
      ],
    );
  }

  String getSelectedColor() {
    return map['month']! > 0 ? '#1D1D2E' : '#B2B2B2';
  }

  void openDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext dialogContext) {
          return BrnDialog(
              titleWidget: Container(
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: Text(
                  '修改费用',
                  style: TextStyle(
                      fontFamily: 'Source Han Sans CN',
                      fontWeight: FontWeight.w500,
                      fontSize: 24,
                      color: ColorUtils.fromHex('#002440')),
                ),
              ),
              contentWidget: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          height: 40,
                          alignment: Alignment.center,
                          child: Text('原费用',
                              style: TextStyle(
                                  color: ColorUtils.fromHex('#002440'),
                                  fontSize: 18,
                                  fontWeight: FontWeight.normal,
                                  fontFamily: 'PingFangSC-Regular')),
                        ),
                        Expanded(
                            child: Container(
                          constraints: const BoxConstraints(
                            maxHeight: 40,
                          ),
                          padding: const EdgeInsets.symmetric(horizontal: 6),
                          child: TextField(
                            controller: TextEditingController()
                              ..text = map['price'].toString(),
                            keyboardType: TextInputType.number,
                            enabled: false,
                            decoration: InputDecoration(
                                contentPadding:
                                    const EdgeInsets.symmetric(horizontal: 10),
                                hintText: '请输入',
                                fillColor: ColorUtils.fromHex('#F8F8FC'),
                                focusedBorder: const OutlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Color.fromRGBO(54, 54, 90, 0.3)),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(6.0))),
                                hintStyle: TextStyle(
                                    color: ColorUtils.fromHex('#B2B2B2'),
                                    fontSize: 14,
                                    fontWeight: FontWeight.normal,
                                    fontFamily: 'PingFangSC-Regular'),
                                border: const OutlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Color.fromRGBO(54, 54, 90, 0.3)),
                                    borderRadius: BorderRadius.all(
                                        Radius.circular(6.0)))),
                            textAlign: TextAlign.left,
                            onChanged: (String value) {},
                          ),
                        )),
                        Container(
                          height: 40,
                          alignment: Alignment.center,
                          child: Text('元',
                              style: TextStyle(
                                  color: ColorUtils.fromHex('#002440'),
                                  fontSize: 18,
                                  fontWeight: FontWeight.normal,
                                  fontFamily: 'PingFangSC-Regular')),
                        )
                      ],
                    ),
                    const SizedBox(
                      height: 6,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          height: 40,
                          alignment: Alignment.center,
                          child: Text('新费用',
                              style: TextStyle(
                                  color: ColorUtils.fromHex('#002440'),
                                  fontSize: 18,
                                  fontWeight: FontWeight.normal,
                                  fontFamily: 'PingFangSC-Regular')),
                        ),
                        Expanded(
                            child: Container(
                          constraints: const BoxConstraints(
                            maxHeight: 40,
                          ),
                          padding: const EdgeInsets.symmetric(horizontal: 6),
                          child: TextField(
                            // controller: TextEditingController()..text = map['price'].toString(),
                            keyboardType: TextInputType.number,
                            cursorColor: Colors.transparent,
                            decoration: InputDecoration(
                                hintText: '请输入',
                                fillColor: ColorUtils.fromHex('#F8F8FC'),
                                helperStyle: TextStyle(
                                    color: ColorUtils.fromHex('#B2B2B2'),
                                    fontSize: 14,
                                    fontWeight: FontWeight.normal,
                                    fontFamily: 'PingFangSC-Regular'),
                                border: const OutlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Color.fromRGBO(54, 54, 90, 0.3)),
                                    borderRadius: BorderRadius.all(
                                        Radius.circular(6.0)))),
                            textAlign: TextAlign.left,
                            onChanged: (String value) {
                              if (value.isNotEmpty) {
                                setState(() {
                                  map['price'] = int.parse(value);
                                });
                              }
                            },
                          ),
                        )),
                        Container(
                          height: 40,
                          alignment: Alignment.center,
                          child: Text('元',
                              style: TextStyle(
                                  color: ColorUtils.fromHex('#002440'),
                                  fontSize: 18,
                                  fontWeight: FontWeight.normal,
                                  fontFamily: 'PingFangSC-Regular')),
                        )
                      ],
                    ),
                  ],
                ),
              ),
              actionsWidget: [
                Container(
                  padding: const EdgeInsets.only(bottom: 10),
                  alignment: Alignment.center,
                  child: MaterialButton(
                      height: 48,
                      minWidth: 153,
                      shape: const RoundedRectangleBorder(
                          side: BorderSide.none,
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      color: ColorUtils.fromHex('#3D7EFE'),
                      child: const Text('确定',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.normal,
                              fontFamily: 'PingFangSC-Regular')),
                      onPressed: () {
                        Navigator.of(context).pop();
                      }),
                )
              ]);
        });
  }
}
